﻿#include "itemadd.h"
#include "ui_itemadd.h"

ItemAdd::ItemAdd(QWidget *parent, const QString &table) :
    QDialog(parent),
    _table(table),
    ui(new Ui::ItemAdd)
{
    ui->setupUi(this);

    isEditMode=false;
    ui->key->setText(KEY_NULL);
    keyType=QMetaEnum::fromType<Qt::Key>();

    isLockKey=false;
    changeState(isLockKey);
}

ItemAdd::~ItemAdd()
{
    delete ui;
}

void ItemAdd::keyPressEvent(QKeyEvent *event)
{
    if(!isLockKey)
    {
        QString keyContext;
        if(event->modifiers() == Qt::AltModifier)
        {
            QString alt=keyType.valueToKey(Qt::Key_Alt);
            QString key=keyType.valueToKey(event->key());
            if(alt == key)
                keyContext=alt;
            else
                keyContext=alt + "+" + key;
        }
        else
            keyContext=keyType.valueToKey(event->key());

        ui->key->setText(keyContext.remove("Key_"));
    }
}

void ItemAdd::on_check_stateChanged(int arg1)
{
    if(ui->key->text() == KEY_NULL)
        return;

    if(arg1 == Qt::Checked)
        isLockKey=true;
    else
        isLockKey=false;

    changeState(isLockKey);
}

void ItemAdd::on_btn_pick_clicked()
{
    QString filePath=QFileDialog::getOpenFileName(this, QStringLiteral("选择图片文件"),QString(),QStringLiteral("图片文件 (*.png *.jpg)"));
    if (filePath.isEmpty())
        return;

    QFileInfo fi(filePath);
    QString fileName=fi.baseName();
    ui->label->setText(fileName);
    ui->detail->setPlainText(PRE_IMG + filePath);
}

void ItemAdd::on_btn_submit_clicked()
{
    QString key=ui->key->text();
    if(key==KEY_NULL)
        return;

    QString label=ui->label->text().trimmed();
    if(label.isEmpty())
        return;

    QString detail=ui->detail->toPlainText();
    if(detail.isEmpty())
        return;

    if(!isEditMode)
    {
        if(!addItem(key,label,detail))
            QMessageBox::warning(this,QStringLiteral("异常情况"),QStringLiteral("无法提交数据！"));
        else
            this->accept();
    }
    else
    {
        if(_label == label && _detail == detail)
            return;

        if(!updateItem(label,detail))
            QMessageBox::warning(this,QStringLiteral("异常情况"),QStringLiteral("无法更新数据！"));
        else
            this->accept();
    }
}

bool ItemAdd::addItem(const QString &key, const QString &label, const QString &detail)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (key,label,detail) values (?,?,?)").arg(_table));
    query.addBindValue(key);
    query.addBindValue(label);
    query.addBindValue(detail);

    if(query.exec())
        return true;
    else
    {
        qDebug() << query.lastError().text();
        return false;
    }
}

bool ItemAdd::updateItem(const QString &label, const QString &detail)
{
    QSqlQuery query;
    QString sql=QString("update %1 set label='%2', detail='%3' where key='%4'")
            .arg(_table)
            .arg(label)
            .arg(detail)
            .arg(_key);

    if(query.exec(sql))
        return true;
    else
    {
        qDebug() << query.lastError().text();
        return false;
    }
}

void ItemAdd::triggerEditMode()
{
    isEditMode=true;
    ui->check->setChecked(true);
    ui->check->setEnabled(false);
}

void ItemAdd::changeState(bool flag)
{
    ui->label->setEnabled(flag);
    ui->detail->setEnabled(flag);
    ui->btn_pick->setEnabled(flag);
    ui->btn_submit->setEnabled(flag);
}

void ItemAdd::setKey(const QString &key)
{
    _key=key;
    ui->key->setText(_key);
}

void ItemAdd::setLabel(const QString &label)
{
    _label=label;
    ui->label->setText(label);
}

void ItemAdd::setDetail(const QString &detail)
{
    _detail=detail;
    ui->detail->setPlainText(detail);
}
